//
//  SISSCoopCommentView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/28.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCommentView.h"
#import "UIView+frameAdjust.h"
#import "SISSCoopCommentMidView.h"

@interface SISSCommentView ()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView *contentView;
/** 弹窗标题 */
@property (nonatomic,copy) NSString *title;

/* midView */
@property (strong, nonatomic) UIView *midView;

@end
@implementation SISSCommentView

- (instancetype)initWithTitle:(NSString *)title midView:(UIView *)midView {
    
    if (self = [super init]) {
        self.title = title;
        self.midView = midView;
        
        // UI搭建
        [self setUpUI];
    }
    return self;
}

// 当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect rect = [self.contentView convertRect:self.contentView.bounds toView:self];
    if ((SCREEN_HEIGHT - rect.origin.y - self.contentView.bounds.size.height) < [SISSPublicSingleton shared].keyboardHeight) {
        
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(-([SISSPublicSingleton shared].keyboardHeight - SCREEN_HEIGHT * 0.5 + 0.5 * self.contentView.bounds.size.height));
        }];
    }
}

// 当键退出
- (void)keyboardWillHide:(NSNotification *)notification {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
    }];
    
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setUpUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.8];
    WeakSelf
    [UIView animateWithDuration:0.1 animations:^{
        weakSelf.alpha = 1;
    }];
    
    // 监听当键盘将要出现时
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    // 监听当键将要退出时
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc]init];
//    self.contentView.frame = CGRectMake((SCREEN_WIDTH - 285) / 2, (SCREEN_HEIGHT - 215) / 2, 300, 400);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 6;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = MAIN_TEXT_COLOR;
    titleLabel.text = self.title;
    [self.contentView addSubview:titleLabel];
    
    // 中间的midView
//    UIView *midView = [[UIView alloc]initWithFrame:CGRectMake(0, titleLabel.maxY + 10, self.contentView.width, 160)];
    [self.contentView addSubview:self.midView];
   

    UIView *horizontalLine = [[UIView alloc] init];
    horizontalLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:horizontalLine];
    // 取消按钮
    UIButton *cancelButton = [[UIButton alloc]init];
    [self.contentView addSubview:cancelButton];
    //    cancelButton.backgroundColor = [UIColor colorWithHexString:@"d51619"];
    [cancelButton setTitleColor:COLOR(0x3578F6) forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    cancelButton.tag = 100;
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *verticalLine = [[UIView alloc] init];
    verticalLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:verticalLine];
    
    // 确定按钮
    UIButton *confirmButton = [[UIButton alloc]init];
    [self.contentView addSubview:confirmButton];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    //    confirmButton.backgroundColor = [UIColor colorWithHexString:@"c8c8c8"];
    [confirmButton setTitleColor:COLOR(0x3578F6) forState:UIControlStateNormal];
    [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    confirmButton.tag = 101;
    [confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    

    CGFloat margin = 8.0;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf);
        make.left.equalTo(weakSelf).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.contentView);
        
    }];
    
    [horizontalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.midView.mas_bottom).offset(2 * margin);
        make.left.right.equalTo(weakSelf.midView);
        make.height.equalTo(@1);
    }];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(horizontalLine.mas_bottom);
        make.right.equalTo(weakSelf.contentView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 0.45, 6 * margin));
        make.bottom.equalTo(weakSelf.contentView);
    }];
    
    [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancelButton.mas_right);
        make.top.equalTo(horizontalLine.mas_bottom);
        make.width.mas_equalTo(1);
        make.height.equalTo(cancelButton);
    }];
    
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(verticalLine.mas_right);
        make.top.equalTo(cancelButton);
        make.size.equalTo(cancelButton);
    }];
    
    
    [self show];
}

#pragma mark - 弹出此弹窗
/** 弹出此弹窗 */
- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

#pragma mark - 移除此弹窗
- (void)dismiss{
    [self removeFromSuperview];
}

#pragma mark - 取消 & 确定按钮按钮点击
- (void)confirmButtonClicked {
    

    
    if ([self.delegate respondsToSelector:@selector(commentViewConfirmButtonClickAction)]) {
        [self.delegate commentViewConfirmButtonClickAction];
    }
    [self dismiss];
}

- (void)cancelButtonClicked {
    
    [self dismiss];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self endEditing:YES];
}

@end
